package com.hera.process.config.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hera.base.util.FiltroDTO;
import com.hera.process.config.dao.DynaformFieldDao;
import com.hera.process.config.dao.FieldOptionDao;
import com.hera.process.config.dao.FormFieldDao;
import com.hera.process.config.service.DynaformFieldService;
import com.hera.process.form.domain.DynaformField;
import com.hera.process.form.domain.DynaformFieldPK;
import com.hera.process.form.domain.FieldOption;

@Service("dynaformfieldservice")
public class DynaformFieldServiceImpl implements DynaformFieldService{

	static final Logger logger = Logger.getLogger(UserServiceImpl.class);

	@Autowired
	private DynaformFieldDao dynaformfielddao;
	@Autowired
	private FormFieldDao formFieldDao;
	@Autowired
	private FieldOptionDao fieldOptionDao;
	
	@Override
	public DynaformField findById(DynaformFieldPK id) {
		// TODO Auto-generated method stub
		return dynaformfielddao.findOne(id);
	}

	@Override
	public void update(DynaformField entity) {
		dynaformfielddao.save(entity);
	}

	@Override
	public void save(DynaformField entity) {
		Integer sequence = dynaformfielddao.getMaxCodePerFormulario(entity.getDynaformFieldPK().getIdDynaform());
		entity.setSequence(sequence);
		dynaformfielddao.save(entity);
		
	}

	@Override
	public void delete(DynaformField entity) {
//	    dynaformfielddao.deleteByFormField(entity.getDynaform().getIdDynaform(), entity.getField().getIdField());
		List<DynaformField> listDynaFormfield = dynaformfielddao.selectByIdFormField(entity.getField().getIdField());
		dynaformfielddao.delete(entity);
		if(listDynaFormfield.size()<=1){
			FiltroDTO filtroDTO = new FiltroDTO();
			filtroDTO.addParametro("formField.idField", entity.getField().getIdField());
			
			List<FieldOption> options= fieldOptionDao.obtenerEntidadesPorFiltros(filtroDTO);
			for(int i=0; i<options.size(); i++){
				fieldOptionDao.delete(options.get(i));
			}
			formFieldDao.delete(entity.getField());
		}
		
		
	}

	@Override
	public List<DynaformField> findAll() {
		// TODO Auto-generated method stub
		return dynaformfielddao.findAll();
	}

	@Override
	public List<DynaformField> findAll(FiltroDTO filtroDTO) {
		// TODO Auto-generated method stub
		return dynaformfielddao.obtenerEntidadesPorFiltros(filtroDTO);
	}

	@Override
	public List<DynaformField> findAllByDynaFormField(FiltroDTO filtroDTO) {
		// TODO Auto-generated method stub
		Long dynaformID=(Long) filtroDTO.getParametro("dynaformID");
		String nombre=(String) filtroDTO.getParametro("nombre");
		
		
		return dynaformfielddao.findAllByDynaFormField(dynaformID,nombre);
	}

	@Override
	public List<DynaformField> findAllByDynaFormFieldByIdDynaForn(
			FiltroDTO filtroDTO) {
		Long dynaformID=(Long) filtroDTO.getParametro("idDynaform");
		return dynaformfielddao.findAllByDynaFormFieldByIdDynaForn(dynaformID);
	}

	@Override
	public void updatewithFormfield(DynaformField dynaformField) {
		// TODO Auto-generated method stub
		if(dynaformField.getField() != null){
			formFieldDao.save(dynaformField.getField());
		}
		dynaformfielddao.save(dynaformField);		
		
	}
	
	


}
